//

#import "MB_TimeView.h"


//
@interface MB_TimeView ()
//
@property (nonatomic, strong) UIView *bgView;           //背景
@property (nonatomic, strong) UIView *tapView;          //白色
@property (nonatomic, strong) UIView *contentView;      //picker
@property (nonatomic, strong) UIDatePicker *datePicker; //
/** */
@property (nonatomic, strong) UIView *topView;          //toor
/** */
@property (nonatomic, strong) UIButton *cancelButton;
/** */
@property (nonatomic, strong) UIButton *sureButton;

@end

@implementation MB_TimeView

//
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initUI];
    }
    
    return self;
}

#pragma mark - Private

- (void)initUI
{
    [self.bgView addSubview:self.tapView];
    [self.contentView addSubview:self.datePicker];
    [self.bgView addSubview:self.contentView];
    [self.contentView addSubview:self.topView];
    [self.topView addSubview:self.cancelButton];
    [self.topView addSubview:self.sureButton];
    
}
//
- (UIView *)bgView {
    if (!_bgView) {
        CGRect rect = [UIApplication sharedApplication].delegate.window.bounds;
        _bgView = [[UIView alloc] initWithFrame:rect];
        _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    }
    return _bgView;
}

//
- (UIView *)tapView {
    if (!_tapView) {
        CGRect rect = [UIApplication sharedApplication].delegate.window.bounds;
        _tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height - 216)];
        _tapView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _tapView.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [_tapView addGestureRecognizer:gesture];
    }
    return _tapView;
}


- (UIView *)contentView {
    if (!_contentView) {
        CGRect rect = [UIApplication sharedApplication].delegate.window.bounds;
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, rect.size.height, rect.size.width, 216)];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}


- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        CGRect rect = [UIApplication sharedApplication].delegate.window.bounds;
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 216)];
        _datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_datePicker addTarget:self
                        action:@selector(changeTime:)
              forControlEvents:UIControlEventValueChanged];
        _datePicker.datePickerMode = UIDatePickerModeDate;
    }
    return _datePicker;
}

- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 40)];
        _topView.backgroundColor = UIColorFromHex(0x9A32CD);
    }return _topView;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 60, 30)];
        [_cancelButton setTitle:@"cancel" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
        
    }return _cancelButton;
}

- (UIButton *)sureButton {
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]initWithFrame:CGRectMake(kMainScreenWidth - 60, 5, 50, 30)];
        [_sureButton setTitle:@"sure" forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_sureButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }return _sureButton;
}


- (void)mbsetDatePickerMode:(UIDatePickerMode)datePickerMode
{
    self.datePicker.datePickerMode = datePickerMode;
}


- (void)mbsetMinimumDate:(NSDate *)minimumDate
{
    self.datePicker.minimumDate = minimumDate;
    
}

#pragma mark - Public

- (void)mbshow {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self.bgView];
    CGRect frame = self.contentView.frame;
    CGRect rect = [UIApplication sharedApplication].delegate.window.bounds;
    frame.origin.y = rect.size.height - frame.size.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.frame = frame;
    } completion:nil];
}


- (void)dismiss {
    CGRect frame = self.contentView.frame;
    CGRect rect = [UIApplication sharedApplication].delegate.window.bounds;
    frame.origin.y = rect.size.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self.bgView removeFromSuperview];
    }];
}

#pragma mark - Action

- (void)changeTime:(UIDatePicker *)datePicker {
    if (self.delegate && [self.delegate respondsToSelector:@selector(mbchangeTime:)]) {
        [self.delegate mbchangeTime:datePicker];
    }
}

- (void)leftButtonAction {
    [self dismiss];
    if (self.delegate && [self.delegate respondsToSelector:@selector(leftButtonAction)]) {
        [self.delegate mbleftButtonAction];
    }
}

- (void)rightButtonAction {
    [self dismiss];
    if (self.delegate && [self.delegate respondsToSelector:@selector(rightButtonAction)]) {
        [self.delegate mbrightButtonAction];
    }
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
